<?php

namespace common\logics\shop;

use Yii;
use common\helpers\Url;
use common\helpers\ColorHelper;
use common\models\shop\ShopCouponScope;

/**
 * 优惠券类型逻辑模型
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class CouponTypeLogic extends \common\base\Model {

    const COLOR_BLUE = '#2865fb';
    const COLOR_YELLOW = '#ddaf64';

    /**
     * @var array 选项卡
     */
    public $store_id;

    /**
     * @var ShopCouponScope 适用范围
     */
    public $scope;

    /**
     * @var array 限领数量
     */
    public $limit;

    /**
     * @var array 折扣数值
     */
    public $discount;

    /**
     * @var array 折扣数值
     */
    public $layout = 'left';

    /**
     * @var array 选项卡
     */
    private $_typeText;

    /**
     * @var array 选项卡
     */
    private $_typeName;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['store_id', 'limit', 'discount'], 'integer'],
            [['layout'], 'string'],
            [['store_id'], 'validateStoreType'],
            [['scope'], 'validateScope'],
            [['discount'], 'validateDiscount'],
        ];
    }

    public function validateStoreType($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        if ($this->_typeName) {
            return;
        }
        if ($this->$attribute) {
            $this->_typeName = 'store';
            $this->_typeText = '店铺优惠券';
        }
    }

    public function validateScope($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        if ($this->_typeName) {
            return;
        }
        $scope = $this->$attribute;
        if ($scope && $scope instanceof ShopCouponScope) {
            $this->_typeName = $scope->name;
            $this->_typeText = '限' . $scope->title;
        }
    }

    public function validateDiscount($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        if ($this->_typeName) {
            return;
        }
        $discount = $this->$attribute;
        $this->_typeName = 'platform';
        if ($this->limit) {
            if ($discount >= 100) {
                $this->_typeText = '满减券';
            } else {
                $this->_typeText = '满折券';
            }
        } else {
            if ($discount >= 100) {
                $this->_typeText = '立减券';
            } else {
                $this->_typeText = '立折券';
            }
        }
    }

    /**
     * 
     */
    public function fields() {
        $this->validate();
        return [
            'name',
            'background',
            'text' => 'title',
            'color',
        ];
    }

    public function getName() {
        return $this->_typeName;
    }

    public function getTitle() {
        return $this->_typeText;
    }

    public function getColor() {
        $color = self::COLOR_BLUE;
        if ($this->_typeName === 'platform') {
            $color = self::COLOR_YELLOW;
        }
        return ColorHelper::format($color);
    }

    public function getBackground() {
        $image = Yii::t('message', '/assets/coupon/{layout}_{filename}.png', [
            'filename' => $this->_typeName,
            'layout' => $this->layout,
        ]);
        return Url::attach($image);
    }

}
